﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace Dora.Tools.Insurance
{
    public class EncryptHelper
    {
        public static string Base64Decode(string data)
        {
            try
            {
                byte[] outputb = Convert.FromBase64String(data);
                string result = System.Text.Encoding.UTF8.GetString(outputb);
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception("Error in base64Decode" + ex.Message);
            }
        }

        public static string StringToHexChar(string data, string charset = "GBK")
        {
            try
            {
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                System.Text.Encoding chset = System.Text.Encoding.GetEncoding(charset);
                byte[] bytes = chset.GetBytes(data.Trim());
                string tempStr = "";
                for (int i = 0; i < bytes.Length; i++)
                {
                    tempStr += string.Format("{0:X}", bytes[i]);
                }
                return tempStr;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public static string HexToString(string data, string charset = "GBK")
        {
            System.Text.Encoding chset = System.Text.Encoding.GetEncoding(charset);
            string strHex = string.Empty;

            byte[] b = new byte[data.Length / 2];
            for (int i = 0; i < b.Length; i++)
            {
                strHex = data.Substring(i * 2, 2);
                b[i] = Convert.ToByte(strHex, 16);
            }
            //按照指定编码将字节数组变为字符串
            return chset.GetString(b);
        }


        #region MD5加密
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="input">需要加密的字符串</param>
        /// <returns></returns>
        public static string MD5Encrypt(string input)
        {
            return MD5Encrypt(input, new UTF8Encoding());
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="input">需要加密的字符串</param>
        /// <param name="encode">字符的编码</param>
        /// <returns></returns>
        public static string MD5Encrypt(string input, Encoding encode)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] t = md5.ComputeHash(encode.GetBytes(input));
            StringBuilder sb = new StringBuilder(32);
            for (int i = 0; i < t.Length; i++)
                sb.Append(t[i].ToString("x").PadLeft(2, '0'));
            return sb.ToString();
        }

        /// <summary>
        /// MD5对文件流加密
        /// </summary>
        /// <param name="sr"></param>
        /// <returns></returns>
        public static string MD5Encrypt(Stream stream)
        {
            MD5 md5serv = MD5CryptoServiceProvider.Create();
            byte[] buffer = md5serv.ComputeHash(stream);
            StringBuilder sb = new StringBuilder();
            foreach (byte var in buffer)
                sb.Append(var.ToString("x2"));
            return sb.ToString();
        }

        /// <summary>
        /// MD5加密(返回16位加密串)
        /// </summary>
        /// <param name="input"></param>
        /// <param name="encode"></param>
        /// <returns></returns>
        public static string MD5Encrypt16(string input, Encoding encode)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string result = BitConverter.ToString(md5.ComputeHash(encode.GetBytes(input)), 4, 8);
            result = result.Replace("-", "");
            return result;
        }
        #endregion
    }
}